﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 文章和轮播图列表服务
    /// </summary>
    public class ArticleListService : IArticleListService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public ArticleListService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="articleTitle">文章标题 模糊查询</param>
        /// <param name="state">文章的状态 -1关闭 0全部 1正常</param>
        /// <param name="value">-1轮播图 0全部 1文章</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public ArticleListDto GetArticleList(string articleTitle, int state, int value, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            ArticleListDto articleListDto = new ArticleListDto();
            articleListDto.ArticleLists = _db.Queryable<ArticleList>()
                .WhereIF(articleTitle != null, x => x.ArticleTitle.Contains(articleTitle))
                .WhereIF(state != 0, x => x.State == state)
                .WhereIF(value != 0, x => x.Value == value)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            articleListDto.Total = totalCount;
            articleListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return articleListDto;
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="articleList">文章实体，id和时间不用传，后端赋值</param>
        /// <returns></returns>
        public bool AddArticle(ArticleList articleList)
        {
            articleList.Time = DateTime.Now.ToString();
            if (_db.Insertable(articleList).ExecuteCommand() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 编辑文章
        /// </summary>
        /// <param name="articleList">文章实体，时间不用传，后端赋值成修改时间</param>
        /// <returns></returns>
        public bool EditArticle(ArticleList articleList)
        {
            articleList.Time = DateTime.Now.ToString();
            return _db.Updateable(articleList).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <returns></returns>
        public bool DelArticle(int articleId)
        {
            return _db.Deleteable<ArticleList>()
                .Where(x => x.ArticleId == articleId)
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <returns></returns>
        public ArticleList GetArticleDetail(int articleId)
        {
            return _db.Queryable<ArticleList>()
                .Where(x => x.ArticleId == articleId)
                .First();
        }
    }
}
